package com.thinvent.nj.ais.controller.sys;

import com.thinvent.nj.common.page.Page;
import com.thinvent.nj.common.rest.ResponseEntity;
import com.thinvent.nj.common.util.CollectionUtil;
import com.thinvent.nj.common.util.MapperUtil;
import com.thinvent.nj.ais.entity.Resource;
import com.thinvent.nj.ais.service.ResourceService;
import com.thinvent.nj.web.controller.BaseViewController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 资源管理controller
 * @author liupeijun
 */
@Controller
@RequestMapping(path = "/sys/resources")
public class ResourceController extends BaseViewController {

    @Autowired
    private ResourceService resourceService;

    @RequiresPermissions("resource:view")
    @RequestMapping(method = RequestMethod.GET)
    public String toHtml() {
        return "sys/resourceList";
    }

    @RequiresPermissions("resource:view")
    @RequestMapping(path = "/search/page", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity findTableData(@RequestBody Map<String, Object> params) {
        Page<Resource> target = resourceService.findPage(getQueryParameter(params), getQueryMsg(params));
        return ResponseEntity.ok(target);
    }

    @RequiresPermissions("resource:view")
    @RequestMapping(path = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity get(@PathVariable("id") String id) {
        Resource target = resourceService.get(id);
        return ResponseEntity.ok(target);
    }

    @RequiresPermissions("resource:add")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity insert(@RequestBody Map<String, Object> params) {
      Resource target = new Resource();
      MapperUtil.copy(params, target);

      resourceService.insert(target);

        return ResponseEntity.ok();
    }

    @RequiresPermissions("resource:update")
    @RequestMapping(path = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody Map<String, Object> params) {
        Resource target = resourceService.get(id);
        MapperUtil.copy(params, target);
        resourceService.update(target);

        return ResponseEntity.ok();
    }

    @RequiresPermissions("resource:delete")
    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity del(@PathVariable("id") String id) {
        resourceService.delete(id);
        return ResponseEntity.ok();
    }

    @RequiresPermissions("resource:view")
    @RequestMapping(path = "/tree", method = RequestMethod.POST)
    public void getResourceTree() {
        List<Resource> targets = resourceService.findAll();
        String ztreeData = CollectionUtil.listToZtree(MapperUtil.convertToListMap(targets), "id", "pId", "name");

        render(ztreeData);
   }
}
